<?php

namespace app\adminapi\controller;

use think\Controller;
use think\Request;
use app\common\model\Admin as AdminModel;

class Admin extends BaseApi
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //kwyword page
        $params = input();
        $where = [];
        if (!empty($params['keyword'])){
            $where['username'] = ['like',"%{$params['keyword']}%"];
        }
        //分页，包含搜索
        $list = AdminModel::where($where)->paginate(2);
        $this->ok($list);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $params = input();
        $validate = $this->validate($params,[
            'username'=>'require|unique:admin,username',
            'nickname'=>'require|unique:admin,nickname,',//后面还可以接排查 1，id
            'email'=>'require|email',
            'role_id'=>'require|integer|gt:0',
            'password'=>'length:6,20',

        ]);
        if ($validate !== true){
            $this->fail($validate);
        }
        if (empty($params['password'])){
            $params['password'] = encrypt_password('123456');
        }
        $row = AdminModel::create($params,true);
        $info = AdminModel::find($row['id']);
        $this->ok($info);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $info = AdminModel::field('id,username,email,nickname,last_login_time,status,role_id')->find($id);
        $this->ok($info);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $params = input();
        $validate = $this->validate($params,[
            'nickname'=>'require|max:50|unique:admin,nickname,',//后面还可以接排查 1，id
            'email'=>'require|email|unique:admin,email,'.$id,
            'role_id'=>'require|integer|gt:0',
            'password'=>'length:6,20',

        ]);
        if ($validate !== true){
            $this->fail($validate);
        }
        if (empty($params['password'])){
            $params['password'] = encrypt_password('123456');
        }
        unset($params['username']);
        $row = AdminModel::update($params,['id',$id],true);
        $info = AdminModel::find($row['id']);
        $this->ok($info);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        if ($id == 1){
            $this->fail('超级管理不能删除');
        }
        //不能删除自己
        $user_id = input('user_id');
        if ($id == $user_id){
            $this->fail('不能删除自己');
        }
        $info = AdminModel::find($id);

        AdminModel::destroy($id);
        $this->ok();
    }
}
